package com.example.lenovo.weiyi;

import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.demon.GetYZMM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.utils.TimeCount;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

import static com.example.lenovo.weiyi.R.id.bt_getma_next;

public class ChangetPhoneGetMaActivity extends BaseActivity {

    @BindView(R.id.ed_register_yzm)
    EditText edRegisterYzm;
    @BindView(R.id.iv_getma_del)
    ImageView ivGetmaDel;
    @BindView(R.id.btn_getma)
    Button btnGetma;
    @BindView(bt_getma_next)
    Button btGetmaNext;
    @BindView(R.id.view_line)
    View viewLine;
    private TimeCount time;
    private String yanzhengma;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_yan_zheng_ma);
        ButterKnife.bind(this);
        AddActivity(this);
        ChangLayTitle("更换手机号");
        LayBack();
        init();
    }

    private void init() {
        btGetmaNext.setText("确定");
        edRegisterYzm.addTextChangedListener(this);
        time = new TimeCount(ChangetPhoneGetMaActivity.this, 60000, 1000, btnGetma);// 构造CountDownTimer对象
    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        super.onTextChanged(s, i, i1, i2);
        if (s.length() > 0) {
            ivGetmaDel.setVisibility(View.VISIBLE);
            btGetmaNext.setBackgroundResource(R.drawable.ed_fullmain);
            btGetmaNext.setClickable(true);
        } else {
            ivGetmaDel.setVisibility(View.GONE);
            btGetmaNext.setBackgroundResource(R.drawable.ed_fullgeray);
            btGetmaNext.setClickable(false);

        }
    }

    @OnClick({R.id.btn_getma, R.id.bt_getma_next, R.id.iv_getma_del})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_getma://获取验证码
                getRegisterYZMData();
                break;
            case R.id.bt_getma_next://确定
                if (TextUtils.isEmpty(edRegisterYzm.getText().toString())) {
                    CommonUtil.showToask(ChangetPhoneGetMaActivity.this, "请输入验证码！");
                    return;
                }
                //对比验证码是否正确
                if (!edRegisterYzm.getText().toString().equals(yanzhengma)) {
                    CommonUtil.showToask(ChangetPhoneGetMaActivity.this, "请输入正确的验证码！");
                    return;
                }
                getData();
                break;
            case R.id.iv_getma_del:
                edRegisterYzm.setText("");
                break;
        }
    }

    /**
     * 获取验证码
     */
    public void getRegisterYZMData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.ChangePhone_getma, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangetPhoneGetMaActivity.this, "uid"));
        mRequest.add("password", PreferencesUtils.getString(ChangetPhoneGetMaActivity.this, "psw"));
        mRequest.add("mobile", getIntent().getStringExtra("phone"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(ChangetPhoneGetMaActivity.this, 0, mRequest,
                new CustomHttpListener(ChangetPhoneGetMaActivity.this, true, GetYZMM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        GetYZMM model = ((GetYZMM) data);
                        time.start();
                        yanzhengma = model.getObject();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(ChangetPhoneGetMaActivity.this, obj.getString("msg"));
                    }
                }, true, true);
    }

    /**
     * 提交
     */
    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.ChangePhone_sure, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangetPhoneGetMaActivity.this, "uid"));
        mRequest.add("password", PreferencesUtils.getString(ChangetPhoneGetMaActivity.this, "psw"));
        mRequest.add("mobile", getIntent().getStringExtra("phone"));
        mRequest.add("smsCode", yanzhengma);
        // 添加到请求队列
        CallServer.getRequestInstance().add(ChangetPhoneGetMaActivity.this, 0, mRequest,
                new CustomHttpListener(ChangetPhoneGetMaActivity.this, true, CommonM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "phone", getIntent().getStringExtra("phone"));
                        ShowPop(model.getMsg());
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
//                        CommonUtil.showToask(ChangetPhoneGetMaActivity.this, obj.getString("msg"));
                    }
                }, true, true);
    }

    /**
     */
    public void ShowPop(String s) {
        // 加载布局
        LayoutInflater inflater = LayoutInflater
                .from(ChangetPhoneGetMaActivity.this);
        View view = inflater.inflate(R.layout.pop_changephone, null);
        TextView tv_notice_title = (TextView) view.findViewById(R.id.tv_pop_change);
        TextView tv_pop_changesure = (TextView) view.findViewById(R.id.tv_pop_changesure);
        tv_notice_title.setText(s);
        // 创建popupwindow对话框
        final PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setFocusable(true);// 必须有，因为要获得焦点进行输入
        // 设置此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 点击其他除了popupwindow对话框的地方就可使popupwindow对话框消失
        pop.setOutsideTouchable(true);
        //pop.showAtLocation(liFr, Gravity.CENTER, 0, 0);// 显示位置以某控件为中心
        if (Build.VERSION.SDK_INT >= 24) {
            int[] a = new int[2];
            viewLine.getLocationInWindow(a);
            pop.showAtLocation((ChangetPhoneGetMaActivity.this).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, a[1] + viewLine.getHeight());
        } else {
            pop.showAsDropDown(viewLine);
        }
        tv_pop_changesure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop.dismiss();
                setcleardata();
                clearActivity();
                clearMainActivity();
                startActivity(LoginActivity.class);
            }
        });
    }

    public void setcleardata() {
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "user_name", "");
//        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "phone", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "psw", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "enable", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "salt", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "danwei", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "bumen", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "email", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "statues", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "url", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "uid", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "age", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "statues", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "IsQD", "");
        PreferencesUtils.putString(ChangetPhoneGetMaActivity.this, "qcard", "");
        PreferencesUtils.putInt(ChangetPhoneGetMaActivity.this, "Login", 0);//登陆标记
    }

}
